<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '.././mixins/resize'
import {
  getCustCount
} from "@/api/crmsys/dashboard/dashboard";

const animationDuration = 2000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  dicts: [
    "crm_customerStatus",
  ],
  data() {
    return {
      chart: null,
      xAxis:null,
      yAxis:null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  created() {
    this.getCustCount();
  }
  ,
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: this.xAxis,
          axisTick: {
            alignWithLabel: true
          },
          axisLabel:{ interval:0 }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '客户数量',
          type: 'bar',
          stack: 'vistors',
          data: this.yAxis,
          animationDuration
        }]
      })
    },
    getCustCount(){
      getCustCount().then(response=>{
        this.xAxis=response.data.xAxis;
        this.yAxis=response.data.yAxis;
        if(response.data.isEmp=="1"){
            for(let item in this.xAxis){
              for(let item2 in this.dict.type.crm_customerStatus){
                if(this.xAxis[item]==this.dict.type.crm_customerStatus[item2].value){
                  this.xAxis[item]=this.dict.type.crm_customerStatus[item2].label;
                }
              }
            }
              console.log(this.dict.type.crm_customerStatus);
        }
        this.initChart();
      });
    },
    getStatus(val){

    }
  }
}
</script>
